// static/js/contract.js
document.addEventListener('DOMContentLoaded', function() {
    // 显示提示消息
    const showAlert = (alertElement) => {
        if (alertElement) {
            alertElement.style.display = 'block';
            setTimeout(() => {
                alertElement.style.display = 'none';
            }, 3000);
        }
    };

    const successAlert = document.querySelector('.alert-success');
    const errorAlert = document.querySelector('.alert-danger');

    showAlert(successAlert);
    showAlert(errorAlert);

    // 逾期合同行悬停效果
    const overdueRows = document.querySelectorAll('.overdue-row');
    overdueRows.forEach(row => {
        row.addEventListener('mouseenter', function() {
            this.style.backgroundColor = '#ffe0e0';
        });
        row.addEventListener('mouseleave', function() {
            this.style.backgroundColor = '#fff0f0';
        });
    });

    // 部门选择变化时自动提交表单
    const departmentSelect = document.querySelector('select[name="departmentId"]');
    if (departmentSelect) {
        departmentSelect.addEventListener('change', function() {
            this.form.submit();
        });
    }
});

document.addEventListener('DOMContentLoaded', function() {
    // 处理合同终止按钮点击
    document.querySelectorAll('.btn-action.terminate').forEach(button => {
        button.addEventListener('click', function(e) {
            e.preventDefault();

            if (confirm('确定要终止此合同吗？此操作不可撤销！')) {
                // 创建隐藏表单提交
                const form = document.createElement('form');
                form.method = 'POST';
                form.action = this.href;

                // 由于禁用了CSRF，不需要添加CSRF token
                document.body.appendChild(form);
                form.submit();
            }
        });
    });
});